<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0056)http://demo.cssmoban.com/cssthemes4/zwtp_2_gi/right.html -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

	<title>giggle后台管理</title>
	<link href="./style.css" rel="stylesheet" type="text/css">
		<script type="text/javascript" src="../js/jquery-3.2.1.min.js"></script>

		<script type="text/javascript">
			var blogs;
			var users;
			var blogRowCount = 0;
			var userRowCount = 0;
			$(document).ready(function() {
				if($('#whichPage').val() == 'blog'){
					show_sub('blog');
				}else{
					show_sub('user');
				}
				

				$(document).on('click', '.pagepre0', function() {
					show_single_page(0, blogs, $(this).text() - 1)
				});

				$(document).on('click', '.pagepre1', function() {
					show_single_page(1, users, $(this).text() - 1)
				});

				$(document).on('click', '.deleteBlog', function() {
					var tr = $(this).parent().parent().first();
					var postid = tr.attr('value');
					$.ajax({
						url : '../post/deletePost',
						type : "POST",
						async : false,
						data : {
							postid : postid
						},
						success : function(data) {
							console.log(data);
							if (data['status'] == -250) {
								window.location.href = "login.html";
							} else if (data['status'] == 0) {
								tr.remove();
								blogRowCount--;
								alert('删除成功');
							} else {
								alert(data['msg']);
							}
						},
						error : function(err) {
							console.log(err);
						}
					});
				});

			});

			function show_sub(v) {
				if (v == 'blog') {
					$("#blogTable").css('display', 'table');
					$("#userTable").css('display', 'none');
					$("#pagin1").css('display', 'block');
					$("#pagin2").css('display', 'none');
					if (blogs == undefined) {
						show_blogs();
					}
				} else if (v == 'user') {
					$("#blogTable").css('display', 'none');
					$("#userTable").css('display', 'table');
					$("#pagin2").css('display', 'block');
					$("#pagin1").css('display', 'none');
					if (users == undefined) {
						show_users();
					}
				}
			}

			function show_blogs() {
				blogs = get_all_blog();
				show_single_page(0, blogs, 0);
				$('#totalPages').text(blogs.length);
				$('#nowPage').text(1);
				$("#blogPagin").empty();
				for (var i = 0; i < Math.ceil(blogs.length / 10); i++) {
					var appendC = '<li class="paginItem"><a href="javascript:;"><span ' 
					+ ' class="pagepre0">'
							+ (i + 1) + '</span></a></li>';
					$("#blogPagin").append(appendC);
				}
			}

			function show_users() {
				users = get_all_user();
				show_single_page(1, users, 0);
				$('#totalPages2').text(users.length);
				$('#nowPage2').text(1);
				$("#userPagin").empty();
				for (var i = 0; i < Math.ceil(users.length / 10); i++) {
					var appendC = '<li class="paginItem"><a href="javascript:;"><span ' 
					+ ' class="pagepre1">'
							+ (i + 1) + '</span></a></li>';
					$("#userPagin").append(appendC);
				}
			}

			function get_all_blog() {
				var blogs;
				$.ajax({
					url : '../post/getAllPosts',
					type : "GET",
					async : false,
					success : function(data) {
						console.log(data);
						if (data['status'] == -250) {
							window.location.href = "login.html";
						} else if (data['status'] == 0) {
							blogs = data['result']['posts'];
						} else {
							alert(data['msg']);
						}
					},
					error : function(err) {
						console.log(err);
					}
				});
				return blogs;
			}

			function get_all_user() {
				var users;
				$.ajax({
					url : '../user/getAllUsers',
					type : "GET",
					async : false,
					success : function(data) {
						console.log(data);
						if (data['status'] == -250) {
							window.location.href = "login.html";
						} else if (data['status'] == 0) {
							users = data['result']['users'];
						} else {
							alert(data['msg']);
						}
					},
					error : function(err) {
						console.log(err);
					}
				});
				return users;
			}

			function show_single_page(type, blogs, page_num) {
				var start = page_num * 10;
				if (type == 0) {
					blogRowCount = 0;
					$("#blogTable tbody").html("");
					for (var i = start; i < start + 10 && i < blogs.length; i++) {
						addBlogRow(blogs[i])
					}
					$('#nowPage').text(page_num + 1);
				} else {
					userRowCount = 0;
					$("#userTable tbody").html("");
					for (var i = start; i < start + 10 && i < users.length; i++) {
						addUserRow(users[i])
					}
					$('#nowPage').text(page_num + 1);
				}

			}

			//添加行  
			function addBlogRow(rowContent) {
				blogRowCount++;
				var newRow = '<tr id="blog'+blogRowCount+'" value=' + rowContent['post']['postid'] + '><td >'
						+ rowContent['post']['postid']
						+ '</td><td>'
						+ rowContent['post']['title']
						+ '</td><td>'
						+ rowContent['publishUser']['userName']
						+ '</td><td>'
						+ rowContent['post']['time']
						+ '</td><td>'
						+ '<a href="../inner_post.html?postid='
						+ rowContent['post']['postid']
						+ '&pageNumber=1" class="tablelink"> 查看</a>'
						+ '<a href="#" class="tablelink deleteBlog"> 删除</a>'
						+ '</td></tr>';
				$('#blogTable').append(newRow);
			}

			//添加行  
			function addUserRow(rowContent) {
				userRowCount++;
				var newRow = '<tr id="user'+userRowCount+'"><td >'
						+ rowContent['userid'] + '</td><td>'
						+ rowContent['userName'] + '</td><td>'
						+ rowContent['gender'] + '</td><td>'
						+ rowContent['phoneNum'] + '</td><td>'
						+ rowContent['description'] + '</td><td>'
						+ '<a href="../other_homepage.html?userid='
						+ rowContent['userid'] + '" class="tablelink"> 查看</a>'
						+ '</td></tr>';
				$('#userTable').append(newRow);
			}
		</script>
</head>
<style>
#parent {
	background: url('http://ourjs.github.io/static/2015/arrow.png') right
		center no-repeat;
	width: 100px;
	height: 30px;
	overflow: hidden;
	border: solid 1px #ccc;
	float: right;
	margin-right: 30px;
}

#parent select {
	-webkit-appearance: none;
	-moz-appearance: none;
	appearance: none;
	background: transparent;
	border: none;
	padding-left: 10px;
	width: 120px;
	height: 100%;
}
</style>

<body>


	<div class="rightinfo">

		<div class="tools">

			<a href="../home_page.html"
				style="font-weight: 700; color: #00F; margin-left: 30px; font-size: 20px;">去主页</a>

			<div id="parent">
				<select id='whichPage'
					onchange="show_sub(this.options[this.options.selectedIndex].value)">
					<option value="blog">博客管理</option>
					<option value="user">用户管理</option>
				</select>
			</div>

		</div>


		<table class="tablelist" id="blogTable">
			<thead>
				<tr>
					<th>id<i class="sort"><img src="./px.gif"></i></th>
					<th>标题</th>
					<th>发帖人</th>
					<th>发布时间</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>



			</tbody>
		</table>

		<table class="tablelist" id="userTable" style="display: none">
			<thead>
				<tr>
					<th>id<i class="sort"><img src="./px.gif"></i></th>
					<th>用户名</th>
					<th>性别</th>
					<th>电话</th>
					<th>个性签名</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>

			</tbody>
		</table>



		<div class="pagin" id="pagin1">
			<div class="message">
				共<i class="blue" id='totalPages'></i>条记录，当前显示第&nbsp;<i class="blue"
					id='nowPage'>2&nbsp;</i>页
			</div>
			<ul class="paginList" id="blogPagin">
				<li class="paginItem"><a href="javascript:;"><span
						class="pagepre"></span></a></li>
				<li class="paginItem"><a href="javascript:;"><span
						class="pagenxt"></span></a></li>
			</ul>
		</div>

		<div class="pagin" id="pagin2" style="display: none">
			<div class="message">
				共<i class="blue" id='totalPages2'></i>条记录，当前显示第&nbsp;<i class="blue"
					id='nowPage2'>&nbsp;</i>页
			</div>
			<ul class="paginList" id="userPagin">
				<li class="paginItem"><a href="javascript:;"><span
						class="pagepre"></span></a></li>
				<li class="paginItem"><a href="javascript:;"><span
						class="pagenxt"></span></a></li>
			</ul>
		</div>


		<div class="tip">
			<div class="tiptop">
				<span>提示信息</span><a></a>
			</div>

			<div class="tipinfo">
				<span><img src="./ticon.png"></span>
				<div class="tipright">
					<p>是否确认对信息的修改 ？</p>
					<cite>如果是请点击确定按钮 ，否则请点取消。</cite>
				</div>
			</div>

			<div class="tipbtn">
				<input name="" type="button" class="sure" value="确定">&nbsp;
					<input name="" type="button" class="cancel" value="取消">
			</div>

		</div>




	</div>

	<script type="text/javascript">
		$('.tablelist tbody tr:odd').addClass('odd');
	</script>


</body>
</html>